package com.processmanager.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * WebSocket配置类
 * 
 * <p>配置WebSocket消息代理，支持实时日志推送：</p>
 * <ul>
 *   <li>启用WebSocket消息代理</li>
 *   <li>配置STOMP端点</li>
 *   <li>设置消息代理前缀</li>
 *   <li>配置客户端订阅前缀</li>
 * </ul>
 * 
 * @author ProcessManager
 * @version 1.0.0
 * @since 2024-01-01
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    /**
     * 配置消息代理
     * 
     * @param registry 消息代理注册器
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        // 启用简单的内存消息代理，用于向客户端发送消息
        registry.enableSimpleBroker("/topic", "/queue");
        
        // 设置客户端发送消息的前缀
        registry.setApplicationDestinationPrefixes("/app");
        
        // 设置用户目标前缀
        registry.setUserDestinationPrefix("/user");
    }

    /**
     * 注册STOMP端点
     * 
     * @param registry STOMP端点注册器
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 注册WebSocket端点，允许跨域访问
        registry.addEndpoint("/ws")
                .setAllowedOriginPatterns("*")
                .withSockJS();
    }
} 